package com.queue;

import java.util.Arrays;

public class MQueue implements Mque{
    public static int size=0;
    @Override
    public void push(int val) {
        QNode newNode=new QNode(val);
        if(head==null){//第一个节点
            head=newNode;
            size++;
            return;
        }
        //其他节点
        QNode cur=head;
        //尾插
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=newNode;
        size++;
    }

    @Override
    public void pop() {
        if(head==null){
            throw new RuntimeException("no elem");
        }
        //头删
        head=head.next;
        size--;
    }

    @Override
    public int peek() {//返回第一个节点的值
        if(head==null){
            throw new RuntimeException("no elem");
        }
        return head.val;
    }

    @Override
    public void info() {
        while(!empty()){
            System.out.println(peek());
            pop();
        }
    }

    @Override
    public boolean empty() {
        return size==0;
    }

    static class QNode{
        int val;
        QNode next;
        public QNode(int val) {
            this.val = val;
            this.next=null;
        }
    }
    public static QNode head;

}
